/*
 * Copyright 2009-2012 Evun Technology. 
 * 
 * This software is the confidential and proprietary information of
 * Evun Technology. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with evun.cn.
 */
package com.squence.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.squence.dao.IdentityDao;
import com.squence.model.IdentityDO;
import com.squence.service.IdentityService;

 
/**
 * 描述: 主键服务的实现
 * 创建人: yangw
 * 创建时间: 2012-5-31 上午11:21:56
 */
@Service
public class IdentityServiceImpl implements IdentityService {

	private static final int DEFAULT_LENGTH = 5;
	private static final String FULL_DATE_MODE = "2";

	@Resource
	private IdentityDao identityDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public Integer updateIndentity(String code) {
		return updateIndentityByCount(code, 1);
	}
	
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public Integer updateIndentityByCount(String code, Integer count) {
		IdentityDO identity = this.identityDao.select(code);
		if(identity == null){
			return 0;
		}
		Integer currentVal = identity.getCurrentValue();
		identity.setCurrentValue(currentVal + count);
		int res = this.identityDao.update(identity);
		if (res > 0) {
			return currentVal;
		} else {
			return 0;
		}
	}
 
	 

}